package ${package.Controller}

import com.lgl.common.Pager
import com.lgl.common.util.CommonUtil
import com.lgl.common.vo.ExportData
import com.lgl.common.vo.Result
import com.lgl.enums.status.YesNo
import ${package.Entity}.${entity}
import ${package.Service}.${table.serviceName}
import com.lgl.erp.controller.base.BaseController
import org.slf4j.LoggerFactory
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController
<#else>
import org.springframework.stereotype.Controller
</#if>
import org.springframework.ui.ModelMap
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import javax.annotation.Resource
import javax.validation.Valid

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${table.controllerPath}<#else>${table.entityPath}</#if>")
class ${table.controllerName}<#if superControllerClass??> : BaseController() {</#if>

    @Resource
    private lateinit var ${serviceNameSimple}Service: ${table.serviceName}

    @RequestMapping
    fun page(map: ModelMap): String {
        map["stores"] = availableStores()
<#list table.fields as field>
<#if field.search && field.select>
        map["${field.propertyName}"] = YesNo.values()
</#if>
</#list>
        return "${controllerMappingHyphenSimple}/list"
    }

    @RequestMapping("/list")
    @ResponseBody
    fun list(pager: Pager<${entity}>, value: ${entity}): Any {
        return ${serviceNameSimple}Service.getList(pager, value)
    }

    @RequestMapping("/edit")
    fun edit(map: ModelMap, idx: Long?): String {
<#list table.fields as field>
    <#if field.editable && field.select>
        map["${field.propertyName}"] = YesNo.values()
    </#if>
</#list>

        if (idx != null) {
            val ${serviceNameSimple} = ${serviceNameSimple}Service.detail(idx)
            map["${serviceNameSimple}"] = ${serviceNameSimple}

            <#list table.fields as field>
            <#if field.propertyName == "locationId">
            initLocation(map, ${serviceNameSimple}!!.locationId!!)
            </#if>
            </#list>
        }

        return "${controllerMappingHyphenSimple}/edit"
    }

    @RequestMapping("/save")
    @ResponseBody
    fun save(@Valid param: ${entity}): Result<Void> {
        ${serviceNameSimple}Service.save(param)
        return Result.SUCCESS()
    }

    @RequestMapping("/detail")
    fun detailPage(map: ModelMap, idx: Long): String {
        map["${serviceNameSimple}"] = ${serviceNameSimple}Service.detail(idx)
        return "${controllerMappingHyphenSimple}/detail"
    }

    @RequestMapping("/{idx}")
    @ResponseBody
    fun detail(@PathVariable("idx") idx: Long): Result<${entity}> {
        val value = ${serviceNameSimple}Service.detail(idx) ?: return Result.ERROR("${table.comment!}不存在或无权访问")
        return Result.SUCCESS(value)
    }

    /**
    * 导出
    */
    @RequestMapping("/export")
    @ResponseBody
    fun export(pager: Pager<${entity}>, bo: ${entity}): Any {
        pager.usePager = false
        pager.order = "asc"
        pager.sort = "createTime"
        ${serviceNameSimple}Service.getList(pager, bo)
        if (CommonUtil.isEmpty(pager.rows)) {
            return CommonUtil.getMessage(2)
        }
        val builder = ExportData.Sheet.Builder("${table.comment!}")
        .header(<#list table.fields as field><#if field.propertyName != "idx">"${field.comment}", </#if></#list>)
        for (vo in pager.rows) {
        val data: MutableList<Any?> = ArrayList()
        <#list table.fields as field><#if field.propertyName == "idx"><#elseif field.propertyType == "LocalDate">
            data.add(CommonUtil.dateToString(vo.${field.propertyName}))
        <#elseif field.propertyType == "LocalDateTime">
            data.add(CommonUtil.dateTimeToString(vo.${field.propertyName}))
        <#else>
            data.add(vo.${field.propertyName})
        </#if></#list>
            builder.add(data)
        }
        return exportData(builder.build())
    }

    @RequestMapping("/delete")
    @ResponseBody
    fun delete(idx: Long): Result<Void> {
        ${serviceNameSimple}Service.delete(idx)
        return Result.SUCCESS()
    }

    companion object {
        private val logger = LoggerFactory.getLogger(${table.controllerName}::class.java)
    }

}
